//
//  CLTableViewDataSource.m
//  CLTableViewProject
//
//  Created by Charles on 16/5/20.
//  Copyright © 2016年 Charles. All rights reserved.
//

#import "CLTableViewDataSource.h"
#import <objc/runtime.h>

@implementation CLTableViewDataSource

- (instancetype)init{
    if (self = [super init]) {
        self.sections = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return self;
}

- (CLTableItem *)tableView:(UITableView *)tableView itemForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (_sections.count > 0) {
        CLTableSection *aSectionObject = [_sections objectAtIndex:(NSUInteger) indexPath.section];
        if ([aSectionObject.items count] > indexPath.row) {
            return [aSectionObject.items objectAtIndex:(NSUInteger) indexPath.row];
        }
    }
    return nil;
}

- (NSIndexPath *)tableView:(UITableView *)tableView indexPathForItem:(CLTableItem *)item {
    if (_sections) {
        for (NSUInteger i = 0; i < _sections.count; ++i) {
            CLTableSection *aSectionObject = [_sections objectAtIndex:i];
            NSUInteger objectIndex = [aSectionObject.items indexOfObject:item];
            
            if (objectIndex != NSNotFound) {
                return [NSIndexPath indexPathForRow:objectIndex inSection:i];
            }
        }
    }
    return nil;
}

#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CLTableSection * mysection = self.sections[section];
    NSInteger count = mysection.items.count;
    return count;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CLTableItem *item = [self tableView:tableView itemForRowAtIndexPath:indexPath];
    item.indexPath = [self tableView:tableView indexPathForItem:item];
    
    Class cellClass = [item viewClass];
    
    NSAssert(cellClass != nil, @"未找到item对应的View class, item:%@", item);
    
    NSString *className =[NSString stringWithUTF8String:class_getName(cellClass)];
    UITableViewCell *cell = (UITableViewCell *) [tableView dequeueReusableCellWithIdentifier:className];
    if (cell == nil) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:className];
    }
    
    CLTableViewCell *srCell = (CLTableViewCell *) cell;
    srCell.indexPath = indexPath;
    srCell.item = item;
    
    [self tableView:tableView cell:cell willAppearAtIndexPath:indexPath];
    
    return cell;

}


#pragma mark TableView Data Update
- (void)tableViewWillReloadModel:(UITableView *)tableView {
    
}

- (void)tableViewDidLoadModel:(UITableView *)tableView {
    [tableView reloadData];
}

- (void)tableView:(UITableView *)tableView cell:(UITableViewCell *)cell willAppearAtIndexPath:(NSIndexPath *)indexPath {
}

- (NSIndexPath *)tableView:(UITableView *)tableView willUpdateItem:(id)item atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willInsertItem:(id)item atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willRemoveItem:(id)item atIndexPath:(NSIndexPath *)indexPath {
    return nil;
}

- (void)tableView:(UITableView *)tableView willAppendItems:(NSArray *)items {
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
@end
